package com.cxg.fairy.ec.main.personal.coupon;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.order.PostOrderDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/4/18.
 */

public class AllCouponListDelegate extends LatteDelegate implements ISuccess{

    public final static String ALL_COUPON_TYPE = "ALL_COUPON_TYPE";

    private AllCouponTabDelegate mAllCouponTabDelegate = null;

    private void setmAllCouponTabDelegate(AllCouponTabDelegate allCouponTabDelegate) {
        this.mAllCouponTabDelegate = allCouponTabDelegate;
    }

    private PostOrderDelegate mPostOrderDelegate = null;

    private void setmPostOrderDelegate(PostOrderDelegate postOrderDelegate) {
        this.mPostOrderDelegate = postOrderDelegate;
    }

    @BindView(R2.id.rv_coupon_list)
    RecyclerView mRecyclerView = null;

    public static AllCouponListDelegate create(int couponType, AllCouponTabDelegate allCouponTabDelegate) {
        Bundle args = new Bundle();
        args.putInt(ALL_COUPON_TYPE, couponType);
        AllCouponListDelegate delegate = new AllCouponListDelegate();
        delegate.setmAllCouponTabDelegate(allCouponTabDelegate);
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_coupon_list;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        RestClient.builder()
                .url("getMsCouponGetByCate")
                .params("uuid", AccountManager.getSignUUID())
                .params("cate", getArguments().getInt(ALL_COUPON_TYPE))
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }


    @Override
    public void onSuccess(String response) {
        LatteLogger.e("response=",response);
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 30);
        mRecyclerView.setLayoutManager(manager);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this.getContext(),DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.my_decoration_single));
        mRecyclerView.addItemDecoration(dividerItemDecoration);

        final List<MultipleItemEntity> data = new CouponListDataConverter().setJsonData(response).convert();
        final AllCouponListAdapter couponListAdapter = new AllCouponListAdapter(data);
        mRecyclerView.setAdapter(couponListAdapter);
    }

}
